$(function(){
   
    var avatar = new Avatar();
    avatar.init().init();
    
});

function Avatar(){
    this.init = function(){
        return AVATAR;
    };
    
    var AVATAR = {
        init : function(){
            this.listeners.bindEvents();
            
            AVATAR.funs.initUpload();
        },
        listeners : {
            bindEvents : function(){
                $.util.foreachFunction(this, [ "bindEvents" ]);
            },
            bindUpLoad : function(){
                $("#a_upload_enter").click(function(){
                    $('#btn_upload').uploadifyUpload();
                });
            }
        },
        funs : {
            initUpload : function(){
                $('#btn_upload').uploadify({
                    'uploader'  : SYSTEM_DATA.p + '/js/plugin/Uploadify/uploadify.swf',
                    'script'    : SYSTEM_DATA.contextPath + '/File/upload',
                    'cancelImg' : SYSTEM_DATA.p + '/js/plugin/Uploadify/cancel.png',
                    'folder'    : '/uploads',
                    'removeCompleted' : true,
                    'auto'       : false,
                    'multi': false,
                    'fileExt' : '*.jpg;*.jpeg;*.png;*.gif',
                    'fileDesc' : '可上传的图片文件',
                    'sizeLimit' : 1024 * 1024 * 1,
                    'buttonText' : 'UpLoad',
                    'onComplete'  : function(event, ID, fileObj, response, data) {
                        //console.debug(response);
                        //console.debug(data);
                        response = $.parseJSON(response);
                        //var lastId = response.data;
                        //                        console.debug(response);
                        //$fileId = $('#hid_file_id');
                        
                        //alert(response.data.path);
                        $.post(SYSTEM_DATA.contextPath + '/User/avatar.shtml', {
                            'id' : $("#a_upload_enter").attr('uId'),
                            'avatar' : response.data.path
                        }, function(jsonResult){
                            if(jsonResult.data){
                                window.location.reload();
                            }
                        }, 'json');
                    },
                    'onError' : function(event,ID,fileObj,errorObj) {
                    //                        console.debug(errorObj);
                    }
                });
            }
        }
    };
}




